package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.ground.UserCarInfoBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3CarAdapter extends RecyclerView.Adapter<X3CarAdapter.CarHolder> {

    private List<UserCarInfoBean> carInfoBeans;

    private Context mContext;

    private String selectStr = "";

    private List<Boolean> selets = new ArrayList<>();
    private final Drawable check;
    private final Drawable no_check;

    public X3CarAdapter(Context context) {
        this.mContext = context;
        this.carInfoBeans = new ArrayList<>();
        check = ContextCompat.getDrawable(mContext, R.drawable.x3_checked);
        no_check = ContextCompat.getDrawable(mContext, R.drawable.x3_checked_false);
    }

    /**
     * 刷新选中
     * @param selectStr
     */
    public void changeSelect(String selectStr) {
        this.selectStr = selectStr;
        if (!X3StringUtils.isEmpty(selectStr)) {
            for (int i = 0; i < carInfoBeans.size(); i++) {
                if (selectStr.equals(carInfoBeans.get(i).getNum())) {
                    selets.remove(i);
                    selets.add(i, true);
                } else {
                    selets.remove(i);
                    selets.add(i, false);
                }
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 刷新全部数据
     * @param carInfoBeans
     */
    public void changeData(List<UserCarInfoBean> carInfoBeans) {
        this.carInfoBeans = carInfoBeans;
        for (int i = 0; i < carInfoBeans.size(); i++) {
            selets.add(false);
        }
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public CarHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CarHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_car_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull CarHolder holder, int position) {
        String num = carInfoBeans.get(position).getNum();
        String carNum = X3StringUtils.isEmpty(num) ? "-" : num;
        holder.tv_car_num.setText(carNum);

        String brandName = X3StringUtils.isEmpty(carInfoBeans.get(position).getCarBrandName()) ? "-" : carInfoBeans.get(position).getCarBrandName();
        String styleName = X3StringUtils.isEmpty(carInfoBeans.get(position).getStyleName()) ? "-" : carInfoBeans.get(position).getStyleName();
        holder.tv_car_style.setText(brandName +"-" +styleName);

        long carBrandId = carInfoBeans.get(position).getBrandId();
//        switch (carBrandId) {
//            case 1:
//                holder.tv_car.setText("奔");
//                break;
//            case 2:
//                holder.tv_car.setText("宝");
//                break;
//            case 3:
//                holder.tv_car.setText("奥");
//                break;
//            case 4:
//                holder.tv_car.setText("大");
//                break;
//            case 5:
//                holder.tv_car.setText("保");
//                break;
//            case 6:
//                holder.tv_car.setText("M");
//                break;
//            case 7:
//                holder.tv_car.setText("非");
//                break;
//        }

        if (selets.get(position)) {
            holder.iv_check.setBackground(check);
        }else {
            holder.iv_check.setBackground(no_check);
        }
        holder.itemView.setOnClickListener(view -> {
            for (int i = 0;i < selets.size(); i++) {
                selets.remove(i);
                if (i == position) {
                    selets.add(i,true);
                }else {
                    selets.add(i,false);
                }
            }
            X3CarAdapter.this.notifyDataSetChanged();

            if (iOnClick != null) {
                iOnClick.iOnClickCar(carInfoBeans.get(position).getNum());
            }
        });
    }

    IOnClick iOnClick;

    public void setiOnClick(IOnClick iOnClick) {
        this.iOnClick = iOnClick;
    }

    public interface IOnClick {
        void iOnClickCar(String carNum);
    }

    @Override
    public int getItemCount() {
        return carInfoBeans.size();
    }

    class CarHolder extends RecyclerView.ViewHolder {
        TextView tv_car_num;
        TextView tv_car_style;
        ImageView iv_check;
        TextView tv_car;
        public CarHolder(@NonNull View itemView) {
            super(itemView);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            tv_car_style = itemView.findViewById(R.id.tv_car_style);
            iv_check = itemView.findViewById(R.id.iv_check);
            tv_car = itemView.findViewById(R.id.tv_car);
        }
    }
}